<?php
/**
 * 空间
 * @author liuxiaoqing <liuxiaoqing@zhishisoft.com>
 * @version TS3.0
 */
class SpaceAction extends Action {
	
	public function _initialize(){
		if(empty($this->uid)) $this->uid = $this->mid;
		if($this->mid == $this->uid){
			$data['space_user'] = $this->user;
		}else{
			$data['space_user'] = model('User')->getUserInfo($this->uid);
		}
		$this->assign($data);
	}
	
	/**
	 * 空间首页
	 * 
	 * @author diccn
	 * 2014-9-7
	 */
	public function index(){
		$data['list'] = D('Works')->where(array('uid'=>$this->uid))->findPage();
		foreach ( $data['list']['data'] as $key=>$val){
			$data['list']['data'][$key]['user_info'] = model('User')->getUserInfo($val['uid']);
			$data['list']['data'][$key]['like_works'] = M('like')
					->where(array('uid'=>$this->mid,'wid'=>$val['id']))->count();
		}
		$this->assign($data);
		$this->display();
	}
	/**
	 * 喜欢的
	 * 
	 * @author diccn
	 * 2014-9-14
	 */
	public function like(){
		$data['list'] = D('Works')->getLikeWorks($this->uid);
		foreach ( $data['list']['data'] as $key=>$val){
			$data['list']['data'][$key]['user_info'] = model('User')->getUserInfo($val['uid']);
			$data['list']['data'][$key]['like_works'] = 1;
		}
		$this->assign($data);
		$this->display();
	}
	/**
	 * 标签
	 *
	 * @author diccn
	 * 2014-9-14
	 */
	public function tags(){
		$sql = "SELECT	t.* , count(wt.wid) as wtotal FROM ts_works_tag AS wt LEFT JOIN ts_tag AS t ON wt.tag_id = t.tag_id WHERE	wt.uid = {$this->uid} GROUP BY	t. NAME";
		$list = M("works_tag")->query($sql);
		$first_letter_array = array_unique(getSubByKey($list,'first_letter'));
		sort($first_letter_array);
		foreach ($first_letter_array as $k=>$v){
			$data['list'][$k]['first_letter'] = $v;
			foreach ($list as $key => $val){
				if( $v == $val['first_letter'] ){
					$val['percentage'] = ceil($val['wtotal'] / $val['total'] * 100) ;
					$data['list'][$k]['tags'][] = $val;
				}
			}
		}
		$this->assign($data);
		$this->display();
	}
	/**
	 * 发布
	 * 
	 * @author diccn
	 * 2014-9-7
	 */
	public function release(){
		$data['user_info'] = model('User')->getUserInfo($this->mid);
		$this->assign($data);
		$this->display();
	}
	/**
	 * 保存作品
	 * 
	 * @author diccn
	 * 2014-9-9
	 */
	public function save(){
		require ADDON_PATH.'/library/simple_html_dom.php';
		$data['title'] = t($_POST['title']);
		$data['content'] = h($_POST['content']);
		$data['tag_names'] = t($_POST['tag_names']);
		if(empty($data['title'])){
			$this->ajaxReturn('',"标题不能为空","0");
		}
		if(empty($data['content'])){
			$this->ajaxReturn('',"内容不能为空","0");
		}
		if(empty($data['tag_names'])){
			$this->ajaxReturn('',"标签不能为空","0");
		}
		$html = str_get_html($data['content']);
		//找到所有图片
		foreach($html->find('img') as $element){
			$images[] =  $element->src;
		}
		$data['cover'] = $images[0];
		$data['images'] = explode(';',$images);
		$data['ctime'] = $data['mtime'] = time();
		$data['uid'] = $this->mid;
		$id = M("works")->add($data);
		if( $id > 0 ){
			$data['tag_names'] = str_replace('，' , ',' , $data['tag_names']);
			D('Tag')->addTags($data['tag_names']);
			$tags = D('Tag')->getTagIds($data['tag_names']);
			unset($data);
			$data['wid'] = $id;
			//删除现有的关联数据标签
			M('works_tag')->where($data)->delete();
			foreach ($tags as $val){
				$data['tag_id'] = $val['tag_id'];
				M('works_tag')->add($data);
			}
			$this->ajaxReturn('',"发布成功","1");
		}else{
			$this->ajaxReturn('',"发布失败","0");
		}
		
	}
	/**
	 * 关注
	 * 
	 * @author diccn
	 * 2014-9-15
	 */
	public function following(){
		$data['list'] = model("Follow")->getFollowingList($this->uid);
		foreach ( $data['list']['data'] as $key=>$val){
			$data['list']['data'][$key]['user_info'] = model('User')->getUserInfo($val['fid']);
			if(!empty($data['list']['data'][$key]['user_info']['user_tag'])){
				$tags = getSubByKey($data['list']['data'][$key]['user_info']['user_tag'],'tag_name');
				$data['list']['data'][$key]['user_info']['user_tag'] = implode(',',$tags);
			}
			$data['list']['data'][$key]['userData'] = model('UserData')->getUserData($var['uid']);
			$data['list']['data'][$key]['work_list'] = model('Works')->getUserNew3($var['uid']);
		}
		$this->assign($data);
		$this->display();
	}
	/**
	 * 粉丝
	 * 
	 * @author diccn
	 * 2014-9-15
	 */
	public function follower(){
		$data['list'] = model("Follow")->getFollowerList($this->uid);
		foreach ( $data['list']['data'] as $key=>$val){
			$data['list']['data'][$key]['user_info'] = model('User')->getUserInfo($val['fid']);
			if(!empty($data['list']['data'][$key]['user_info']['user_tag'])){
				$tags = getSubByKey($data['list']['data'][$key]['user_info']['user_tag'],'tag_name');
				$data['list']['data'][$key]['user_info']['user_tag'] = implode(',',$tags);
			}
			$data['list']['data'][$key]['userData'] = model('UserData')->getUserData($val['uid']);
			$data['list']['data'][$key]['work_list'] = model('Works')->getUserNew3($val['uid']);
		}
		$this->assign($data);
		$this->display();
	}
}